{{ define "/admin/role/privilege" }}
<!doctype html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>权限</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="stylesheet" href="/static/admin/css/font.css">
    <link rel="stylesheet" href="/static/admin/css/xadmin.css">
    <script type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/admin/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/admin/js/common.js"></script>

    <link rel="stylesheet" href="/static/admin/css/font.css">
    <link rel="stylesheet" href="/static/lib/layui/css/eleTree.css" media="all">
    <link rel="stylesheet" href="/static/lib/ztree/css/metroStyle/metroStyle.css" type="text/css">
    <link rel="stylesheet" href="/static/admin/css/font.css">
    <link rel="stylesheet" href="/static/lib/layui/css/eleTree.css" media="all">
    <link rel="stylesheet" href="/static/lib/ztree/css/metroStyle/metroStyle.css" type="text/css">
    <script type="text/javascript" src="/static/lib/ztree/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="/static/lib/ztree/js/jquery.ztree.excheck.js"></script>

    <style>
        .layui-table-cell {
            height:auto;
            overflow:visible;
            text-overflow:inherit;
            white-space:normal;
        }
        .left-nav {
            overflow: auto;
        }
        .btn-normal {
            margin: 0 5px;
            color: #1E9FFF;
            cursor: pointer;
        }
        .btn-danger {
            margin: 0 5px;
            color: #FF5722;
            cursor: pointer;
        }
        .btn-warm {
            margin: 0 5px;
            color: #FFB800;
            cursor: pointer;
        }

        .ztree * {
            font-size: 14px;
        }
    </style>
</head>

<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <ul id="purviewTree" class="ztree"></ul>
                        </div>
                        <div class="layui-form-item">
                            <input type="hidden" name="id" value="{{ .id }}">
                            <button class="layui-btn" lay-filter="add" lay-submit="">
                                分配管理权限
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="rowBar">
    <button class="layui-btn layui-btn-xs" lay-event="rowBarEdit">编辑</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="rowBarDel">删除</button>
</script>

<script type="text/html" id="customBar">
    <a class="layui-btn layui-btn-xs" lay-event="customBarAdd">添加</a>
</script>

<script>
    var zTreeObj;
    var zTree = $.fn.zTree;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        view: {
            showIcon: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "ps", "N": "s"}
        },
    };

    var data = "{{ .list }}"
    data = eval('(' + data + ')');

    $(document).ready(function () {
        zTreeObj = $.fn.zTree.init($("#purviewTree"), setting, data);
        zTreeObj.expandAll(true)
    });

    layui.config({ base: "/static/lib/layui/lay/mymodules/"}).use(['jquery', 'form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(add)', function (obj) {
            var treeObj = zTree.getZTreeObj("purviewTree");
            var nodes = zTreeObj.transformToArray(treeObj.getNodes());

            var purviewIds = [];
            for (i = 0; i < nodes.length; i++) {
                if (nodes[i].checked) {
                    purviewIds.push(nodes[i].id)
                }
            }
            $.ajax({
                type: "post",
                url: obj.form.action,
                data: {
                    'id': $('input[name=id]').val(),
                    'purview_ids': purviewIds
                },
                dataType: "json",
                success: function (res) {
                    if (res.code != 0) {
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 2000//2秒关闭（如果不配置，默认是3秒）
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            xadmin.close();
                        })
                    }
                }
            })
            return false;
        })
    })

</script>
</html>
{{ end }}